chartView.tsx 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. 'use client'
  2. import React, { useState } from 'react'
  3. import dayjs from 'dayjs'
  4. import quarterOfYear from 'dayjs/plugin/quarterOfYear'
  5. import { useTranslation } from 'react-i18next'
  6. import useSWR from 'swr'
  7. import { fetchAppDetail } from '@/service/apps'
  8. import type { PeriodParams } from '@/app/components/app/overview/appChart'
  9. import { AvgResponseTime, AvgSessionInteractions, ConversationsChart, CostChart, EndUsersChart, TokenPerSecond, UserSatisfactionRate } from '@/app/components/app/overview/appChart'
  10. import type { Item } from '@/app/components/base/select'
  11. import { SimpleSelect } from '@/app/components/base/select'
  12. import { TIME_PERIOD_LIST } from '@/app/components/app/log/filter'
  13. dayjs.extend(quarterOfYear)
  14. const today = dayjs()
  15. const queryDateFormat = 'YYYY-MM-DD HH:mm'
  16. export type IChartViewProps = {
  17. appId: string
  18. }
  19. export default function ChartView({ appId }: IChartViewProps) {
  20. const detailParams = { url: '/apps', id: appId }
  21. const { data: response } = useSWR(detailParams, fetchAppDetail)
  22. const isChatApp = response?.mode === 'chat'
  23. const { t } = useTranslation()
  24. const [period, setPeriod] = useState<PeriodParams>({ name: t('appLog.filter.period.last7days'), query: { start: today.subtract(7, 'day').format(queryDateFormat), end: today.format(queryDateFormat) } })
  25. const onSelect = (item: Item) => {
  26. setPeriod({ name: item.name, query: item.value === 'all' ? undefined : { start: today.subtract(item.value as number, 'day').format(queryDateFormat), end: today.format(queryDateFormat) } })
  27. }
  28. if (!response)
  29. return null
  30. return (
  31. <div>
  32. <div className='flex flex-row items-center mt-8 mb-4 text-gray-900 text-base'>
  33. <span className='mr-3'>{t('appOverview.analysis.title')}</span>
  34. <SimpleSelect
  35. items={TIME_PERIOD_LIST.map(item => ({ value: item.value, name: t(`appLog.filter.period.${item.name}`) }))}
  36. className='mt-0 !w-40'
  37. onSelect={onSelect}
  38. defaultValue={7}
  39. />
  40. </div>
  41. <div className='grid gap-6 grid-cols-1 xl:grid-cols-2 w-full mb-6'>
  42. <ConversationsChart period={period} id={appId} />
  43. <EndUsersChart period={period} id={appId} />
  44. </div>
  45. <div className='grid gap-6 grid-cols-1 xl:grid-cols-2 w-full mb-6'>
  46. {isChatApp
  47. ? (
  48. <AvgSessionInteractions period={period} id={appId} />
  49. )
  50. : (
  51. <AvgResponseTime period={period} id={appId} />
  52. )}
  53. <TokenPerSecond period={period} id={appId} />
  54. </div>
  55. <div className='grid gap-6 grid-cols-1 xl:grid-cols-2 w-full mb-6'>
  56. <UserSatisfactionRate period={period} id={appId} />
  57. <CostChart period={period} id={appId} />
  58. </div>
  59. </div>
  60. )
  61. }